package org.mijia.dotsbox.core;

import java.util.List;

import org.mijia.dotsbox.core.model.Entity;

public class BaseDao<T, Key> implements Dao<T, Key> {

	private Class<T> persistentClass;
	private EntityManager entityManager;
	
	public BaseDao(Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}
	
	@SuppressWarnings("unchecked")
	public T find(Key id) {
		return (T) entityManager.find(persistentClass, id);
	}

	@SuppressWarnings("unchecked")
	public List<Key> findAll() {
		return (List<Key>) entityManager.findAll(persistentClass);
	}

	public void remove(Key id) {
		entityManager.remove(persistentClass, id);
	}

	@SuppressWarnings("unchecked")
	public T save(T object) {
		return (T) entityManager.update((Entity) object);
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager manager) {
		entityManager = manager;
	}

}
